<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>猜数字练习</title>
</head>
<body>
<h3>已生成一个1~100之间的数字</h3>
<input type="text" id="sz"><br><br>
<button onclick="f1()">确定</button>
<h3>小提示：<span></span></h3>
<script>
    var c=parseInt(Math.random()*100+ 1);
    console.log('答案是'+c)
    var d=0;
    function f1(){
        d++;
        var a= document.getElementById('sz').value;
        if(!/^\d+$/.test(a)){
            alert('请输入数字');
            return;
        }
        var htmlSpanElement = document.querySelector('span');
        if (a>c){
            htmlSpanElement.innerHTML='猜大了';

        }else if (a<c){
            htmlSpanElement.innerHTML='猜小了';

        }else {
            htmlSpanElement.innerHTML='恭喜你猜对了,一共猜了'+d+'次'
            return;
        }
    }
</script>
</body>
</html>